package com.xhsj.user.sms.purchase.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.sms.purchase.entity.TbPurchaseOrder;
import com.xhsj.user.sms.purchase.entity.TbPurchaseOrderGoods;
import com.xhsj.user.sms.purchase.entity.TbPurchaseSubtaskList;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseOrderGoodsMapper;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseOrderMapper;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseSubtaskListMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 采购订单-主表 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-04-23
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbPurchaseOrder")
public class TbPurchaseOrderService extends BaseService<TbPurchaseOrderMapper, TbPurchaseOrder> {
    @Autowired
    private TbPurchaseOrderMapper dao;

    @Autowired
    private TbPurchaseOrderGoodsMapper tbPurchaseOrderGoodsMapper;

    @Autowired
    private TbPurchaseSubtaskListMapper tbPurchaseSubtaskListMapper;

    @Override
    //@CacheEvict(cacheNames="TbPurchaseOrder", allEntries=true)
    public boolean save(TbPurchaseOrder entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbPurchaseOrder getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    //@CacheEvict(cacheNames="TbPurchaseOrder", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbPurchaseOrder> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbPurchaseOrder entity = JSON.parseObject(JSON.toJSONString(data), TbPurchaseOrder.class);
        entity.setSubmitStatus(null);
        Page<TbPurchaseOrder> page = new Page(currentPage,pageSize);
//        QueryWrapper<TbPurchaseOrder> wrapper = new QueryWrapper(entity);
        IPage<TbPurchaseOrder> IPage = dao.findOrderList(page,entity);
        return IPage;
    }

    @Override
    //@Cacheable(value = "TbPurchaseOrder",sync = true)
    public List<TbPurchaseOrder> selectAll() {
        return super.selectAll();
    }


    /**
     * 通过id获取采购订单
     * @author 苏小林
     * @date 2020/4/28 14:27
     * @param: id
     * @return: java.lang.Object
     */
    public TbPurchaseOrder getPurchaseOrderById(String id) {
        return dao.getPurchaseOrderById(id);
    }


    public List<TbPurchaseOrder> getPurchaseOrderListByTaskId(String taskId) {
        List<TbPurchaseOrder> list = dao.getPurchaseOrderListByTaskId(taskId);
//        for (TbPurchaseOrder tbPurchaseOrder : list) {
//            List<TbPurchaseOrderFile> purchaseOrderFile = tbPurchaseOrder.getPurchaseOrderFile();
//            for (TbPurchaseOrderFile tbPurchaseOrderFile : purchaseOrderFile) {
//                Integer type = tbPurchaseOrderFile.getType();
//                switch (type) {
//                    case 0:
//                        break;
//                }
//
//            }
//
//
//        }
        return list;
    }

    public int updateAuditStatus(String dataId) {
       return dao.updateAuditStatus(dataId);
    }

    public TbPurchaseOrder getPurchaseOrderInfoById(String id) {
        TbPurchaseOrder purchaseOrderById = this.getPurchaseOrderById(id);
        return purchaseOrderById;
    }


    public List<TbPurchaseOrderGoods> getOrderGoodsList(String orderId) {
        List<TbPurchaseOrderGoods> orderGoodsList = tbPurchaseOrderGoodsMapper.getOrderGoodsByOrderId(orderId);

        List<TbPurchaseSubtaskList> subtaskLists = tbPurchaseSubtaskListMapper.getSubtaskListByOrderId(orderId);
        if (subtaskLists !=null && subtaskLists.size()>0) {

            Iterator<TbPurchaseOrderGoods> iterator = orderGoodsList.iterator();
            while (iterator.hasNext()) {
                TbPurchaseOrderGoods next = iterator.next();
                for (TbPurchaseSubtaskList subtaskList : subtaskLists) {
                    if (next.getId().equals(subtaskList.getOrderGoodsId())) {
                        if (next.getApplyNumber().equals(subtaskList.getApplyNumber())) {
                            iterator.remove();
                        }else{
                            Integer oldApplyNumber = next.getApplyNumber();
                            Integer newApplyNumber = subtaskList.getApplyNumber();
                            next.setApplyNumber(oldApplyNumber-newApplyNumber);
                        }
                    }
                }
            }
        }

        return orderGoodsList;
    }

    public List<TbPurchaseOrder> getPurchaseOrderByTaskId(String dataId) {
        return dao.getPurchaseOrderByTaskId(dataId);
    }
}
